# -*- coding: utf-8 -*-
"""
乒乓球培训管理系统 - 校区模型
管理多个培训校区的基本信息和设置
"""

from datetime import datetime, time
from flask_sqlalchemy import SQLAlchemy

from app import db


class Campus(db.Model):
    """
    校区模型
    管理校区基本信息、营业时间、联系方式等
    """
    __tablename__ = 'campuses'

    # 基础字段
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    name = db.Column(db.String(100), nullable=False, comment='校区名称')
    address = db.Column(db.String(255), nullable=False, comment='校区地址')

    # 联系信息
    contact_name = db.Column(db.String(50), nullable=True, comment='联系人')
    phone = db.Column(db.String(20), nullable=True, comment='联系电话')
    email = db.Column(db.String(100), nullable=True, comment='联系邮箱')

    # 管理员关联
    admin_id = db.Column(db.BigInteger, nullable=True, comment='校区管理员ID')

    # 扩展信息
    capacity = db.Column(db.Integer, nullable=True, comment='容纳人数')
    area = db.Column(db.Numeric(8,2), nullable=True, comment='场地面积(平方米)')
    description = db.Column(db.Text, nullable=True, comment='校区描述')
    facilities = db.Column(db.Text, nullable=True, comment='设施设备')
    operating_hours = db.Column(db.String(100), nullable=True, comment='营业时间')
    photo_url = db.Column(db.String(255), nullable=True, comment='校区照片URL')

    # 状态管理
    status = db.Column(db.String(20), nullable=False, default='active', comment='状态:active/inactive/deleted')

    # 审计字段
    created_at = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')
    updated_at = db.Column(db.DateTime, nullable=False, default=datetime.now,
                          onupdate=datetime.now, comment='更新时间')

    # 关联关系
    tables = db.relationship('Table', lazy='dynamic', cascade='all, delete-orphan')

    def __init__(self, **kwargs):
        super(Campus, self).__init__(**kwargs)

    def get_admin(self):
        """获取校区管理员"""
        return self.users.filter_by(role='campus_admin').first()

    def get_students_count(self):
        """获取学员数量"""
        return self.users.filter_by(role='student').count()

    def get_coaches_count(self):
        """获取教练数量"""
        return self.users.filter_by(role='coach').count()

    def get_tables_count(self):
        """获取球台数量"""
        return self.tables.count()

    def get_available_tables(self):
        """获取可用球台列表"""
        return self.tables.all()

    def get_students(self):
        """获取校区学员列表"""
        return self.users.filter_by(role='student').all()

    def get_coaches(self):
        """获取校区教练列表"""
        return self.users.filter_by(role='coach').all()

    def get_pending_coaches(self):
        """获取待审核教练列表"""
        return self.users.filter_by(role='coach').all()

    def to_dict(self):
        """转换为字典格式"""
        return {
            'id': self.id,
            'name': self.name,
            'address': self.address,
            'contact_name': self.contact_name,
            'phone': self.phone,
            'email': self.email,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'students_count': self.get_students_count(),
            'coaches_count': self.get_coaches_count(),
            'tables_count': self.get_tables_count()
        }

    def __repr__(self):
        return f'<Campus {self.name}>'